<template>
  <n-drawer v-model:show="isDrawer" :width="width" :placement="placement">
    <n-drawer-content :title="title" closable>
      <n-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="100"
      >
        <n-form-item label="类型" path="type">
          <span>{{ formParams.menuType === 1 ? '侧边栏菜单' : '' }}</span>
        </n-form-item>
        <n-form-item label="标题" path="menuTitle">
          <n-input placeholder="请输入标题" v-model:value="formParams.menuTitle" />
        </n-form-item>
        <n-form-item label="访问路径" path="menuPath">
          <n-input placeholder="请输入访问路径" v-model:value="formParams.menuPath" />
        </n-form-item>
        <n-form-item label="组件路径" path="componentPath">
          <n-input placeholder="请输入组件路径" v-model:value="formParams.componentPath" />
        </n-form-item>
        <n-form-item label="菜单排序" path="menuSort">
          <n-input placeholder="请输入菜单排序" v-model:value="formParams.menuSort" />
        </n-form-item>
        <n-form-item label="打开方式" path="openWay">
          <n-radio-group v-model:value="formParams.openWay" name="openType">
            <n-space>
              <n-radio value="1">当前窗口</n-radio>
              <n-radio value="2">新窗口</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>
        <n-form-item label="菜单权限" path="emnuPermissions">
          <n-input
            placeholder="请输入权限，多个权限用，分割"
            v-model:value="formParams.emnuPermissions"
          />
        </n-form-item>
        <n-form-item label="隐藏侧边栏" path="hidden">
          <n-switch v-model:value="formParams.hidden" />
        </n-form-item>
      </n-form>

      <template #footer>
        <n-space>
          <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
          <n-button @click="handleReset">重置</n-button>
        </n-space>
      </template>
    </n-drawer-content>
  </n-drawer>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, toRefs } from 'vue';
import { useMessage } from 'naive-ui';

import { add } from '@/api/system/menu';

const rules = {
  menuTitle: {
    required: true,
    message: '请输入标题',
    trigger: 'blur',
  },
  menuPath: {
    required: true,
    message: '请输入路径',
    trigger: 'blur',
  },
};
export default defineComponent({
  name: 'CreateDrawer',
  components: {},
  props: {
    title: {
      type: String,
      default: '添加顶级菜单',
    },
    parentMenuId: {
      type: String,
      default: null,
    },
    width: {
      type: Number,
      default: 450,
    },
  },
  setup(props, context) {
    const message = useMessage();
    const formRef: any = ref(null);
    const defaultValueRef = () => ({
      parentMenuId: null,
      menuTitle: '',
      menuPath: '',
      menuType: '0',
      componentPath: '',
      menuSort: 0,
      openWay: '1',
      emnuPermissions: '',
      hidden: false,
    });

    const state = reactive({
      isDrawer: false,
      subLoading: false,
      formParams: defaultValueRef(),
      placement: 'right',
      alertText:
        '该功能主要实时预览各种布局效果，更多完整配置在 projectSetting.ts 中设置，建议在生产环境关闭该布局预览功能。',
    });

    function openDrawer() {
      state.formParams.parentMenuId = props.parentMenuId;
      state.isDrawer = true;
    }

    function closeDrawer() {
      state.isDrawer = false;
      context.emit('close');
    }

    function formSubmit() {
      formRef.value.validate((errors) => {
        if (!errors) {
          add(state.formParams).then((res) => {
            if (res.success) {
              message.success('添加成功');
              handleReset();
              closeDrawer();
            }
          });
        } else {
          message.error('请填写完整信息');
        }
      });
    }

    function handleReset() {
      formRef.value.restoreValidation();
      state.formParams = Object.assign(state.formParams, defaultValueRef());
    }

    return {
      ...toRefs(state),
      formRef,
      rules,
      formSubmit,
      handleReset,
      openDrawer,
      closeDrawer,
    };
  },
});
</script>
